package cn.fl.preinsroute.controller;

import cmm.comm.facade.framework.api.ISysRegionApiService;
import cmm.comm.facade.framework.dto.SysRegionDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.insroute.vo.InsRoutePrpsFeeVO;
import cn.fl.preinsroute.condition.PreRouteBscInfoQC;
import cn.fl.preinsroute.constant.PreRouteConstant;
import cn.fl.preinsroute.service.IPreRouteBscInfoService;
import cn.fl.preinsroute.service.IPreRouteInsResService;
import cn.fl.preinsroute.vo.PreRouteBscInfoVO;
import cn.fl.preinsroute.vo.PreRouteInsResVO;
import cn.fl.preinsroute.vo.PreRoutePrdInfoVO;
import cn.fl.preinsroute.vo.PreRoutePrpsFeeVO;
import cn.fl.project.condition.PrjBscInfoQueryCondition;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.constant.PrjQuatPrpsConstant;
import cn.fl.project.constant.PrjQuatPrpsFeeConstant;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjQuatPrpsCfgMService;
import cn.fl.project.service.IVehicleSrcService;
import cn.fl.project.service.IVehicleStyleService;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.project.vo.PrjPrdParamConfigVO;
import cn.fl.ref.fribaseinfo.service.IFriBaseInfoService;
import cn.fl.ref.fribaseinfo.utils.FundRasInsNmChangeUtil;
import cn.fl.ref.fribaseinfo.vo.FriBaseInfoVO;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysDictionaryService;
import cn.fl.system.vo.SysDictionaryVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fl.flow.facade.flow.api.IFlowQueryApiService;
import fl.flow.facade.flow.condition.SysFlowSHQueryCondition;
import fl.flow.facade.flow.constant.FlowConstant;
import fl.flow.facade.flow.dto.SysFlowSHDTO;
import fl.ref.fribaseinfo.condition.FriBaseInfoQueryCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 资管路由预筛选基础信息表 控制层
 */
@Controller
@RequestMapping("/preinsroute/preRouteBscInfo")
public class PreRouteBscInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PreRouteBscInfoController.class);

	/** 预筛选service */
	@Autowired
	IPreRouteBscInfoService preRouteBscInfoService ;
	/** 项目基础信息service */
	@Autowired
	private IPrjBscInfoService prjBscInfoService;
	@Autowired
	private IPrjQuatPrpsCfgMService prjQuatPrpsCfgMService;

	@Autowired
	private IVehicleStyleService vehicleStyleService;

	@Autowired
    IVehicleSrcService vehicleSrcService ;
	/**
	 * 客户 服务层BEAN
	 */
	@Autowired
	private ICstBscInfoService cstBscInfoService;//客户基础信息表
	/**
	 * 数据字典
	 */
	@Autowired
	private ISysDictionaryService sysDictionaryService;

	@Autowired
	private IFriBaseInfoService friBaseInfoService;//融资机构基础信息表

    @Autowired
    ISysRegionApiService sysRegionApiService ;

	@Autowired
	private IFlowQueryApiService flowQueryApiService;

	@Autowired
	private IPreRouteInsResService preRouteInsResService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("preinsroute/preRouteBscInfo/preRouteInfoList");
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("preinsroute/preRouteBscInfo/preRouteInfoAddPage");
		String id = request.getParameter("id");
		String editFlag = request.getParameter("editFlag");
		if(StringUtils.isNotEmpty(id) && editFlag.equals("false")){
			view.addObject("editFlag",false);
		}else{
			view.addObject("editFlag",true);
		}
		// 费用计算基数 （只要合同额、融资额）
		List<KeyValueVO> calBaseCdList = this.prjQuatPrpsCfgMService.searchCalBaseCdList();
		List<KeyValueVO> calBaseCdListNew = new ArrayList<KeyValueVO>();
		if(CollectionUtils.isNotEmpty(calBaseCdList)){
			for (KeyValueVO keyValueVO : calBaseCdList) {
				String key = keyValueVO.getKey();
				if(StringUtils.isNotBlank(key) && (PrjQuatPrpsConstant.CODE_CAL_BASE_CONTRACT.equals(key) || PrjQuatPrpsConstant.CODE_CAL_BASE_FINANCING.equals(key))){
					calBaseCdListNew.add(keyValueVO);
				}
			}
		}
		view.addObject("calBaseCdList", JSON.toJSONString(calBaseCdListNew));
		// 是否
		List<KeyValueVO> yesNoCdList = this.prjQuatPrpsCfgMService.searchYesOrNoCdList();
		view.addObject("yesOrNoCdList",JSON.toJSONString(yesNoCdList));
		//是否包牌
		List<KeyValueVO> isBpList = this.prjQuatPrpsCfgMService.searchIsBpListList();
        view.addObject("isBpList",isBpList);
        // 经销商收款账户类型
        List<KeyValueVO> bankNoTypCdList = sysDictionaryService.getKvList("SPL0005", CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        view.addObject("bankNoTypCdList",JSON.toJSONString(bankNoTypCdList));
        //品牌
        List<KeyValueVO> comboBoxeList = this.vehicleStyleService.getBrandList();
        view.addObject("comboBoxeList",JSON.toJSONString(comboBoxeList));
		// 用途与产品类型映射
		List prdTypKVList = new ArrayList();
		List<SysDictionaryVO> dictionaryVOList = sysDictionaryService.searchGroup("CVM0001", "15000", "0000");
		for (SysDictionaryVO vo:dictionaryVOList){
			if (vo.getCodeLvl() == 2 && vo.getIsValid() == 1 && vo.getIsDel() == 0){
				KeyValueVO kv = new KeyValueVO();
				kv.setKey(vo.getCode());
				kv.setValue(vo.getCodeAttr1());
				prdTypKVList.add(JSON.toJSONString(kv));
			}
		}
		view.addObject("prdTypKVList",prdTypKVList);

		// 省份列表
        List<SysRegionDTO> provinceList = this.sysRegionApiService.searchProvinceList("11000");
        List<KeyValueVO> provinceKVList = new ArrayList<>();
        for (SysRegionDTO vo:provinceList){
            KeyValueVO kv = new KeyValueVO();
            kv.setKey(vo.getRgnCd());
            kv.setValue(vo.getRgnNm());
            provinceKVList.add(kv);
        }
        view.addObject("provinceKVList",JSON.toJSONString(provinceKVList));
		// 如果id不为空
		if (StringUtils.isNotEmpty(id)){
			PreRouteBscInfoVO preRouteBscInfoVO = preRouteBscInfoService.selectPreRouteBscInfoAllById(Long.valueOf(id));
			BaseLogUtils.newWebLogger("跳转到详情页面,routeId:" + id + ",preRouteBscInfoVO:" + JSON.toJSONString(preRouteBscInfoVO)) ;
			Long cstId = preRouteBscInfoVO.getCstId();
			CstBscInfoVO cstBscInfoVO = this.cstBscInfoService.selectById(cstId);
			String cstTypCd = cstBscInfoVO.getCstTypCd();
			view.addObject("data",preRouteBscInfoVO);
			view.addObject("cstTypCd",cstTypCd);
            //预筛选机构名称后添加业务线
            List<PreRoutePrdInfoVO> preRoutePrdInfoVOListTemp = preRouteBscInfoVO.getPreRoutePrdInfoVOS();
            //业务线字典List
            List<KeyValueVO> bussinessLineDicList = sysDictionaryService.getKvList("REF0048","11000","0000");
            //金融机构List
            List<FriBaseInfoVO> friBaseInfoVOList = this.friBaseInfoService.searchList(new FriBaseInfoQueryCondition());
            //根据金融机构业务线,设置金融机构名称展示字段值
            FundRasInsNmChangeUtil.insNmShowAddBussinessLineDic(friBaseInfoVOList,bussinessLineDicList);
            BaseLogUtils.newWebLogger("跳转到详情页面,routeId:" + id + ",preRoutePrdInfoVOListTemp 添加业务线before:" + JSON.toJSONString(preRoutePrdInfoVOListTemp)
                    + ",friBaseInfoVOList:" + JSON.toJSONString(friBaseInfoVOList)) ;
            if (CollectionUtils.isNotEmpty(friBaseInfoVOList) && CollectionUtils.isNotEmpty(preRoutePrdInfoVOListTemp)) {
                for (FriBaseInfoVO friBaseInfoVOTemp : friBaseInfoVOList) {
                    if (StringUtils.isNotEmpty(friBaseInfoVOTemp.getId()) && StringUtils.isNotEmpty(friBaseInfoVOTemp.getFundRasInsNmShow())) {
                        for (PreRoutePrdInfoVO preRoutePrdInfoVOTemp : preRoutePrdInfoVOListTemp) {
                            PreRouteInsResVO preRouteInsResVOTemp = preRoutePrdInfoVOTemp.getPreRouteInsResVO();
                            if (preRouteInsResVOTemp != null && StringUtils.isNotEmpty(preRouteInsResVOTemp.getInsId())
                                && friBaseInfoVOTemp.getId().longValue() == preRouteInsResVOTemp.getInsId().longValue()) {
                                preRouteInsResVOTemp.setInsNm(friBaseInfoVOTemp.getFundRasInsNmShow());
                            }
                        }
                    }
                }
            }
            BaseLogUtils.newWebLogger("跳转到详情页面,routeId:" + id + ",preRoutePrdInfoVOListTemp 添加业务线after:" + JSON.toJSONString(preRoutePrdInfoVOListTemp)
                    + ",friBaseInfoVOList:" + JSON.toJSONString(friBaseInfoVOList)) ;
			view.addObject("preRoutePrdInfoVOS", JSON.toJSONString(preRoutePrdInfoVOListTemp));
			view.addObject("preRouteInsResVOS",JSON.toJSONString(preRouteBscInfoVO.getPreRouteInsResVOS()));
		}
		view.addObject("preRoutePrpsFeeVOS",JSON.toJSONString(initInsRoutePrpsFeeVOList()));
		return view;
	}
    /**
     *
     * @param id 获取车源详细信息
     * @return
     */
    @RequestMapping("/carSourceDetail.do")
    @ResponseBody
    public PrjPrdParamConfigVO carDetail(Long id, String prjTypCd){
        // 查询车型具体参数信息
        PrjPrdParamConfigVO prdDetail = this.vehicleSrcService.getPrdDetail(id,prjTypCd,CurrentThreadContext.getCurrentBuOrgCd());
        return prdDetail;
    }
	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String cstNm = request.getParameter("cstNm");
		String cstMgrNm = request.getParameter("cstMgrNm");
		String crtTmStart = request.getParameter("crtTmStart");
		String crtTmEnd = request.getParameter("crtTmEnd");
		String insSts = request.getParameter("insSts");
		String index = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		Pager pager = new Pager();
		PreRouteBscInfoQC qc = new PreRouteBscInfoQC();
		if (StringUtils.isNotEmpty(id)){
			qc.setPreName(id);
		}
		if (StringUtils.isNotEmpty(cstNm)){
			qc.setCstNm(cstNm);
		}
		if (StringUtils.isNotEmpty(cstMgrNm)){
			qc.setCstMgrNm(cstMgrNm);
		}
		if (StringUtils.isNotEmpty(crtTmStart)){
			qc.setCrtTmStart(DateUtils.formatStringToDate(crtTmStart, DateUtils.YYYY_MM_DD));
		}
		if (StringUtils.isNotEmpty(crtTmEnd)){
			qc.setCrtTmEnd(new Date(DateUtils.formatStringToDate(crtTmEnd, DateUtils.YYYY_MM_DD).getTime() + 24*60*60*1000));
		}
		if(StringUtils.isNotEmpty(insSts)){
			qc.setInsSts(Integer.valueOf(insSts));
		}
		qc.setUsrId(CurrentThreadContext.getCurrentUserId());
		pager.setCurrentPage(Integer.valueOf(index));
		pager.setEveryPage(Integer.valueOf(pageSize));
		pager.setCondition(qc);
		pager = preRouteBscInfoService.searchListPage(pager);
		List<PreRouteBscInfoVO> list = (List<PreRouteBscInfoVO>) pager.getResultList();
		ListPageVO<PreRouteBscInfoVO> listPage = new ListPageVO<PreRouteBscInfoVO>(list,pager.getTotalCount());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.preRouteBscInfoService.removeById(Long.valueOf(id));
			BaseLogUtils.newWebLogger("删除数据.[id="+id+"]").info();
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.newWebLogger( "删除数据失败.[id="+id+"]"+ex.getMessage()).setThrowable(ex).error(LogExceptionEnum.COMMON_CODE_0001);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.newWebLogger("删除数据失败.[id="+id+"]"+ex.getMessage()).setThrowable(ex).error(LogExceptionEnum.COMMON_CODE_0001);
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.newWebLogger("获取数据,主键不存在.[id="+id+"]").info();
			return result;
		}
		
		try {
			PreRouteBscInfoVO vo = this.preRouteBscInfoService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.newWebLogger("获取数据,数据不存在.[id="+id+"]").info();
			}
			result.setData(vo);
			BaseLogUtils.newWebLogger("获取数据.[id="+id+"]").info();
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.newWebLogger("获取失败.[id="+id+"]"+ex.getMessage()).setThrowable(ex).error(LogExceptionEnum.COMMON_CODE_0001);
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.newWebLogger("获取失败.[id="+id+"]"+ex.getMessage()).setThrowable(ex).error(LogExceptionEnum.COMMON_CODE_0001);
		}
		return result;
	}

	/**
	 * 新增
	 */
	
	@ResponseBody
	@RequestMapping(value = "/addOrModify.do", method = { RequestMethod.POST })
	public DataResultVO add(@RequestBody PreRouteBscInfoVO preRouteBscInfoVO) throws BaseException {
		BaseLogUtils.newWebLogger("编辑预筛选信息.[id="+preRouteBscInfoVO.getId()+"] DATA:" + JSONObject.toJSONString(preRouteBscInfoVO)).info();
		DataResultVO dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		Long routeId = null ;
		BaseLogUtils.newWebLogger("保存预筛选基本信息：" + JSON.toJSONString(preRouteBscInfoVO)).info();
		if (preRouteBscInfoVO == null){
            dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            dataResultVO.setData(null);
            dataResultVO.setInfo("参数为空");
            return null ;
        }
		preRouteBscInfoVO.setCstMgrId(CurrentThreadContext.getCurrentUserId());
		preRouteBscInfoVO.setCstMgrNm(CurrentThreadContext.getCurrentUserName());
		preRouteBscInfoVO.setCstMgrOrgCd(CurrentThreadContext.getCurrentOrgCd());
		preRouteBscInfoVO.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		preRouteBscInfoVO.setDsCd(WebBaseConstant.CHANNEL_CD);
		// 费用项判空
		for (PreRoutePrdInfoVO preRoutePrdInfoVO :preRouteBscInfoVO.getPreRoutePrdInfoVOS()){
			for (PreRoutePrpsFeeVO preRoutePrpsFeeVO : preRoutePrdInfoVO.getPreRoutePrpsFeeVOS()){
				if (preRoutePrpsFeeVO.getFeePct() == null || preRoutePrpsFeeVO.getFeeAmt() == null){
					preRoutePrpsFeeVO.setFeePct(0d);
					preRoutePrpsFeeVO.setFeeAmt(0d);
				}
			}
		}
		BaseLogUtils.newWebLogger("费用项为空处理：" + JSON.toJSONString(preRouteBscInfoVO)).info();

		if (preRouteBscInfoVO.getId() == null){
			routeId = preRouteBscInfoService.add(preRouteBscInfoVO);
			BaseLogUtils.newWebLogger("修改失败.[id="+preRouteBscInfoVO.getId()+"] DATA:" + JSONObject.toJSONString(preRouteBscInfoVO)).info();
		}else{
			routeId = preRouteBscInfoService.modify(preRouteBscInfoVO);
			BaseLogUtils.newWebLogger("修改失败.[id="+preRouteBscInfoVO.getId()+"] DATA:" + JSONObject.toJSONString(preRouteBscInfoVO)).info();
		}
		dataResultVO.setData(routeId);
		dataResultVO.setInfo("保存成功");
		return dataResultVO;
	}

	/**
	 * 到选择用户页面
	 */
	@RequestMapping(value = "/toChooseCstPage.do", method = { RequestMethod.GET })
	public ModelAndView toChooseCstPage(HttpServletRequest request,
										HttpServletResponse response) {
		BaseLogUtils.newWebLogger("加载选择客户列表页面.").info();
		String cstRlFlag = request.getParameter("cstRlFlag");
		String cstTypCd = request.getParameter("cstTypCd");
		ModelAndView view = new ModelAndView(
				"preinsroute/preRouteBscInfo/chooseCstPage");

		// 客户状态
		List<KeyValueVO> cststscdlist = this.cstBscInfoService
				.searchCstStsCdList();
		view.addObject("searchSelectCststscdlist", cststscdlist);
		// 客户类型
		List<KeyValueVO> csttypcdlist = this.cstBscInfoService
				.searchCstTypCdList();
		view.addObject("searchSelectCsttypcdlist", csttypcdlist);
		// 事业部
		List<KeyValueVO> orgList = this.cstBscInfoService
				.searchBusinessOrgList();
		view.addObject("orgListList", orgList);
		// 客户关系属性
		List<KeyValueVO> cstRlTypCdList = this.cstBscInfoService
				.searchCstRlTypList();
		view.addObject("cstRlTypCdList", cstRlTypCdList);
		// 查询项要有默认值,这里给出默认值到前端页面显示
		view.addObject("currentBuOrgCd",
				CurrentThreadContext.getCurrentBuOrgCd());
		view.addObject("currentCstRlType",
				DictionaryConstant.CODE_GRP_CST_RL_TYP_CD_1);
		view.addObject("cstRlFlag", cstRlFlag);
		view.addObject("cstTypCd", cstTypCd);
		return view;
	}

    /**
     * 通过routeId查询预筛选结果
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "searchPrerouteResult.do",method = RequestMethod.GET)
	public JSONObject searchPrerouteResult(@RequestParam("routeId") Long routeId){
        JSONObject result = new JSONObject();
	    if (routeId == null){
	        BaseLogUtils.newWebLogger("查询预筛选结果参数为空：routeId").info();
        }

        PreRouteBscInfoVO preRouteBscInfoVO = preRouteBscInfoService.selectPreRouteBscInfoAllById(routeId);
	    if (preRouteBscInfoVO == null){
	        BaseLogUtils.newWebLogger("查询预筛选结果失败routeId:" + routeId).info();
	        result.put("success",BaseConstant.IS_YESNO_NO);
	        result.put("info","查询预筛选结果失败routeId:" + routeId);
	        return result ;
        }
	    BaseLogUtils.newServiceLogger("查询预筛选结果routeId:" + routeId + ",preRouteBscInfoVO:" +JSON.toJSONString(preRouteBscInfoVO)).info();
        List<PreRouteInsResVO> preRouteInsResVOS = preRouteBscInfoVO.getPreRouteInsResVOS();

        //预筛选机构名称后添加业务线
		//业务线字典List
		List<KeyValueVO> bussinessLineDicList = sysDictionaryService.getKvList("REF0048","11000","0000");
		//金融机构List
		List<FriBaseInfoVO> friBaseInfoVOList = this.friBaseInfoService.searchList(new FriBaseInfoQueryCondition());
		//根据金融机构业务线,设置金融机构名称展示字段值
		FundRasInsNmChangeUtil.insNmShowAddBussinessLineDic(friBaseInfoVOList,bussinessLineDicList);
		if (CollectionUtils.isNotEmpty(preRouteInsResVOS) && CollectionUtils.isNotEmpty(friBaseInfoVOList)) {
			FundRasInsNmChangeUtil.insNmAddBussLineDicForPreRouteInfo(preRouteInsResVOS,friBaseInfoVOList);
		}
		BaseLogUtils.newServiceLogger("查询预筛选结果routeId:" + routeId + ",preRouteInsResVOS:" +JSON.toJSONString(preRouteInsResVOS)).info();

		if (preRouteBscInfoVO.getInsSts().intValue() == 0){
            result.put("success",BaseConstant.IS_YESNO_YES);
            result.put("info","筛选中，请稍候……routeId:" + routeId);
        }else{
			result.put("success",BaseConstant.IS_YESNO_YES);
			result.put("info","筛选成功！");
			result.put("rows",preRouteInsResVOS);
			result.put("total",preRouteInsResVOS.size());
		}
		result.put("insSts",preRouteBscInfoVO.getInsSts());
		return result ;
    }

	/**
	 * 新增初始化费用项数据
	 * @return
	 * @throws BaseException
	 */
	private List<InsRoutePrpsFeeVO> initInsRoutePrpsFeeVOList() throws BaseException {
		List<InsRoutePrpsFeeVO> insRoutePrpsFeeVOList = new ArrayList<InsRoutePrpsFeeVO>();
		InsRoutePrpsFeeVO insRoutePrpsFeeVO01 = new InsRoutePrpsFeeVO();
		insRoutePrpsFeeVO01.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_01);
		insRoutePrpsFeeVO01.setFeePct(null);
		insRoutePrpsFeeVO01.setFeeAmt(null);
		insRoutePrpsFeeVO01.setCalBaseCd(PrjQuatPrpsConstant.CODE_CAL_BASE_CONTRACT);
		insRoutePrpsFeeVO01.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_DOWNPAY);
		insRoutePrpsFeeVO01.setIsAllowChg(BaseConstant.IS_YESNO_YES);
		insRoutePrpsFeeVO01.setIsFixedAmt(BaseConstant.IS_YESNO_NO);
		insRoutePrpsFeeVO01.setFeeTypCdNm("首期");
		insRoutePrpsFeeVO01.setCalBaseCdNm("合同额");
		insRoutePrpsFeeVO01.setIsSysDef(BaseConstant.IS_YESNO_YES);
		insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO01);

		InsRoutePrpsFeeVO insRoutePrpsFeeVO02 = new InsRoutePrpsFeeVO();
		insRoutePrpsFeeVO02.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_02);
		insRoutePrpsFeeVO02.setFeePct(null);
		insRoutePrpsFeeVO02.setFeeAmt(null);
		insRoutePrpsFeeVO02.setCalBaseCd(PrjQuatPrpsConstant.CODE_CAL_BASE_FINANCING);
		insRoutePrpsFeeVO02.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_DOWNPAY);
		insRoutePrpsFeeVO02.setIsAllowChg(BaseConstant.IS_YESNO_YES);
		insRoutePrpsFeeVO02.setIsFixedAmt(BaseConstant.IS_YESNO_NO);
		insRoutePrpsFeeVO02.setFeeTypCdNm("客户保证金");
		insRoutePrpsFeeVO02.setCalBaseCdNm("融资额");
		insRoutePrpsFeeVO02.setIsSysDef(BaseConstant.IS_YESNO_YES);
		insRoutePrpsFeeVOList.add(insRoutePrpsFeeVO02);

		return insRoutePrpsFeeVOList;
	}

	/**
	 * 判断当前项目对应的筛选记录下是否存在筛选结果无对应的项目信息或项目信息为待提交/预审核中/预审核通过/驳回呈报人
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/checkOtherPreResStatus.do", method = {RequestMethod.POST,RequestMethod.GET})
	private JSONObject checkOtherPreResStatus(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		JSONObject result = new JSONObject();
		result.put("success",BaseConstant.IS_YESNO_NO);
		result.put("info","当前预筛选ID无符合的预筛选结果信息！");

		String insCodeNm = ""; //需要返回的机构列表
		String message = ""; //返回页面展示的信息
		String preRouteId = request.getParameter("preRouteId"); //预筛选记录Id
		String preResId = request.getParameter("preResId"); //预筛选结果Id
		if (StringUtils.isEmpty(preRouteId) || StringUtils.isEmpty(preResId)) {
			BaseLogUtils.newWebLogger("预筛选记录ID或者预筛选结果ID为空").info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "预筛选记录ID或者预筛选结果ID为空.");
		}
		//获取预筛选记录下的筛选结果信息
		PreRouteBscInfoVO preRouteBscInfoVO = preRouteBscInfoService.selectPreRouteBscInfoAllById(Long.valueOf(preRouteId));
        BaseLogUtils.newServiceLogger("1.获取预筛选记录下的筛选信息:" + preRouteId + ",preRouteBscInfoVO:" +JSON.toJSONString(preRouteBscInfoVO)).info();
		if (null == preRouteBscInfoVO) {
			BaseLogUtils.newWebLogger("获取预筛选记录下的筛选结果信息为空").info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "获取预筛选记录下的筛选结果信息为空.");
		}
		//获取预筛选记录对应的筛选结果信息
		List<PreRouteInsResVO> preRouteInsResVOList = preRouteBscInfoVO.getPreRouteInsResVOS();
        BaseLogUtils.newServiceLogger("2.获取预筛选记录下的筛选结果信息:" + preRouteId + ",preRouteInsResVOList:" +JSON.toJSONString(preRouteInsResVOList)).info();
        if (CollectionUtils.isEmpty(preRouteInsResVOList)) {
			BaseLogUtils.newWebLogger("获取预筛选记录对应的筛选结果信息为空").info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "获取预筛选记录对应的筛选结果信息为空.");
		}
		for (PreRouteInsResVO preRouteInsResVO : preRouteInsResVOList) {
			if (preRouteInsResVO.getId().longValue() != Long.valueOf(preResId).longValue()) {
				//预筛选状态为空,跳出该预筛选结果
			    if (null == preRouteInsResVO.getResSts()) {
                    continue;
                }
			    //预筛选结果未占用
                if (PreRouteConstant.PRE_ROUTE_INS_STS_1.intValue() == preRouteInsResVO.getResSts().intValue()) {
                    if (StringUtils.isEmpty(insCodeNm)) {
                        insCodeNm += preRouteInsResVO.getInsNm();
                    }  else {
                        insCodeNm += "/" + preRouteInsResVO.getInsNm();
                    }
                    continue;
                } else if (PreRouteConstant.PRE_ROUTE_INS_STS_2.intValue() == preRouteInsResVO.getResSts().intValue()) {
                    /**
                     * 预筛选结果已占用,需判断项目状态
                     * 0-待提交,4-驳回呈报人,7-预审审核中,8-预审审核通过
                     */
                    PrjBscInfoQueryCondition prjBscInfoQC = new PrjBscInfoQueryCondition();
                    prjBscInfoQC.setPreRouteId(Long.valueOf(preRouteId));
                    prjBscInfoQC.setPreResId(preRouteInsResVO.getId());
                    List<PrjBscInfoVO> prjBscInfoVOList = prjBscInfoService.searchPrjBscInfoList(prjBscInfoQC);
                    BaseLogUtils.newServiceLogger("3.获取预筛选记录下已占用的筛选结果信息:" + preRouteId + ",prjBscInfoVOList:" +JSON.toJSONString(prjBscInfoVOList)).info();
					if (CollectionUtils.isNotEmpty(prjBscInfoVOList)) {
						for (PrjBscInfoVO prjBscInfoVO : prjBscInfoVOList) {
							boolean dealFlag = false;
							if (null == prjBscInfoVO.getPreRouteId() || null == prjBscInfoVO.getPreResId()) {
								continue;
							}
							if (prjBscInfoVO.getPreRouteId().longValue() == Long.valueOf(preRouteId).longValue() &&
									prjBscInfoVO.getPreResId().longValue() != Long.valueOf(preResId).longValue()) {
								//查询项目对应的流程状态
								SysFlowSHQueryCondition sysFlowSHQC = new SysFlowSHQueryCondition();
								sysFlowSHQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
								sysFlowSHQC.setDataId(prjBscInfoVO.getId());
								sysFlowSHQC.setWordsInTheTable(PrjBscInfoConstant.PRJ_FLOW_S_H_PRJ);
								sysFlowSHQC.setStatus(BaseConstant.IS_YESNO_YES);
								sysFlowSHQC.setBusTyp(FlowConstant.FLOW_MODEL_TYP_PRJ_1);
								List<SysFlowSHDTO> flowSHDTOList = this.flowQueryApiService.searchFlowSHList(sysFlowSHQC);
								logger.info("3.1 查询项目对应的流程状态 项目ID={},查询条件sysFlowSHQC={},结果flowSHDTOList={}"
										,prjBscInfoVO.getId(),JSON.toJSONString(sysFlowSHQC),JSON.toJSONString(flowSHDTOList));
								if (CollectionUtils.isNotEmpty(flowSHDTOList)) {
									String audStsCdTemp = flowSHDTOList.get(0).getAudStsCd(); //项目流程状态
									if (StringUtils.isNotEmpty(audStsCdTemp) && ("7".equals(audStsCdTemp) || "8".equals(audStsCdTemp) || "4".equals(audStsCdTemp))) {
										dealFlag = true;
									}
								} else {
									//流程状态结果为空表示项目处理待提交状态
									dealFlag = true;
								}
								if (dealFlag) {
									//机构名称从预筛选结果中取值
									PreRouteInsResVO preRouteInsResVOTemp = this.preRouteInsResService.selectById(prjBscInfoVO.getPreResId());
									BaseLogUtils.newLogger("3.2 查询项目预筛选结果信息 项目ID={},PreResId={},preRouteInsResVOTemp={}"
											,prjBscInfoVO.getId(),prjBscInfoVO.getPreResId(),JSON.toJSONString(preRouteInsResVOTemp)).info();
									if (preRouteInsResVOTemp == null) {
										continue;
									}
									if (StringUtils.isEmpty(insCodeNm)) {
										insCodeNm += preRouteInsResVOTemp.getInsNm();
									}  else {
										insCodeNm += "/" + preRouteInsResVOTemp.getInsNm();
									}
									continue;
								}
							}
						}
					}
                }
			}
		}
		BaseLogUtils.newServiceLogger("4.预筛选记录ID:" + preRouteId + ",返回机构列表insCodeNm:" +insCodeNm).info();
		//返回成功,表示有符合的信息
		if (StringUtils.isNotEmpty(insCodeNm)) {
			result.put("success",BaseConstant.IS_YESNO_YES);
			message = "当前项目对应的筛选记录：" + preRouteId + "，尚有 " + insCodeNm + " 机构没有发起评审流程或者提交初审，请尽快发起提交";
			result.put("info",message);
		}
		return result;
	}

}

